Skip to main content

Introduction to Python

This section will cover the introduction to Python.

Table of Contents
  • Overview
  • Python Basics
    • Strings
    • Variables
    • Operators

Overview

Python is a general purpose, object-oriented scripting language. It is mainly used to build applications, automate tasks, and data analysis.

Python Basics

This part will cover some fundamental knowledge required when scripting in Python.

Strings

A string is used to represent textual data. A string is a sequence of characters enclosed in single quotes (' ') or double quotes (" "). The below are some examples of strings.

'My String'
"My String in double quotes."
"string"

To visualise the data, we can print it to the terminal or screen using the print() function.

print("Hello World!")
print('Hello World with single quotes.')

From the above example, the use of single or double quotes works the same.

Variables

Variables can be used to store data. Variables do not need to be declared with any particular type, and can change type after they have been set. Multiple variables can also be declared on the same line.

The below are some examples of variables.

x = 10       #The variable 'x' is an int (integer).
x = "hello" #The variable 'x' is now an str (string).
var1, myvar = 'My first variable', 'my second variable'

We can visualise this with the following lines of code.

x = 10
print(x)

x = "hello"
print(x)

var1, myvar = 'My first variable', "my second variable"
print(var1)
print(myvar)

Running the above code, we can see that the data within the variable can change without any type declaration.

Operators

Python can perform math operations. An example will be using the + operator to add two values.

print(2 + 2)

The below table will list Python arithmetic operators. Arithmetic operations are used with numeric values to perform common math operations.

OperatorNameExampleDescription
+Addition10 + 10Add two or more numbers.
-Substration10 - 5Substrate two or more numbers.
*Multiplication5 * 2Multiply two or more numbers.
/Division10 / 2Divides two or more numbers.
%Modulusx % yFind the remainder after dividing the numbers.
**Exponenta ** bMultiply the base with its power.
//Floor Division11 // 10Round down to the nearest whole number.

Python is also able to perform comparison using the below operators.

OperatorNameExampleDescription
==Equalsx == yChecks if the value is the same. Return "True" if it is the same.
!=Not Equalx != yChecks if the value is different. Return "True" if it is different.
>Greater Thanx > yCheck if the value is larger. Return "True" if it is larger.
<Lesser Thanx < yChecks if the value is smaller. Return "True" if it is smaller.
>=Greater than or equal tox >= yCheck if the value is greater or equal to the value. Return "True" if it is greater or equal to.
<=Less than or equal tox <= yCheck if the value is lesser or equal to the value. Return "True" if it is lesser or equal to.

The below code will help visualise this.

x = 10
y = 5
z = 3

print( x + y) #Results in 15
print(x - y) #Results in 5
print(x * y) #Results in 50
print(x / z) #Results in 3.3333333333333335
print(x % z) #Results in 1
print(x ** y) #Results in 100,000
print(x // z) #Results in 3

print(x == y) #Results in False
print(x != y) #Results in True
print(x > y) #Results in True
print(x < y) #Results in False
print(x >= y) #Results in True
print(x <= y) #Results in False